$(document).ready(function() {	
	findAllDeptType();
	findAllRole();
	// 选择部门窗体的定义
	$("#deptChooseWindow").dialog({
				//position: 'top' ,
				autoOpen : false,
				minWidth : 700,
				modal : true,
				title : '选择所属部门'
	});
	// 选择部门确定
	$("#selDeptOK").click(function() {
		var udIds = new Array();
		var udNames = new Array();
		$("#deptRestult").empty();
		$("#selDepts option").each(function() {
			udIds.push($(this).val());
			udNames.push("  "+$(this).text()+"  ");
		});
		$("#deptRestult").append("<span id='ud_udIds' style='display:none'>"+udIds+"</span>");
		$("#deptRestult").append("<span id='ud_udNames' style='color: #960;'>"+udNames+"</span>");
		$("#deptChooseWindow").dialog('close');
	});
	// 选择部门取消
	$("#selDeptCancel").click(function() {
		$("#deptChooseWindow").dialog('close');
	});
	// 选择角色窗体的定义
	$("#roleChooseWindow").dialog({
				//position: 'top' ,
				autoOpen : false,
				minWidth : 700,
				modal : true,
				title : '选择所属角色'
	});
	// 选择角色确定
	$("#selRoleOK").click(function() {
		var rIds = new Array();
		var rNames = new Array();
		$("#roleRestult").empty();
		$("#selRoles option").each(function() {
			rIds.push($(this).val());
			rNames.push("  "+$(this).text()+"  ");
		});
		$("#roleRestult").append("<span id='r_rIds' style='display:none'>"+rIds+"</span>");
		$("#roleRestult").append("<span id='r_rNames' style='color: #960;'>"+rNames+"</span>");
		$("#roleChooseWindow").dialog('close');
	});
	// 选择角色取消
	$("#selRoleCancel").click(function() {
		$("#roleChooseWindow").dialog('close');
	});
	
	// 弹出任务部门指派的选择窗体
	$('#selectDept').click(function() {
		$("#deptChooseWindow").dialog("open");
	});
	$("#selectRole").click(function() {
		 $("#roleChooseWindow").dialog("open");
	});
	
	// 选择部门的窗体上两个按钮的响应函数 addDept 和 delDept
	$("#addDept").click(function() {
		if ($("#subDepts option:selected").length > 0) {
			$("#subDepts option:selected").each(function() {
				var strHtml="";
				var id="",name=""
				var udId = $(this).val();
				var udName = $(this).text();
				/*
				if($("#selDepts option").length == 1){
					alert("只能选择一个人！");
					return ;
				}
				*/
				if ($("#selDepts option").length > 0 ) {
					var test = true;
					$("#selDepts option").each(function(){
						if(udId == $(this).val()){ 
							test = false;
						}
					})
					if(test){
						id=udId;
						name = udName
						strHtml+="<option class='class1' name='selDepts' selected value='" + udId + "'>"
							+ udName + "</option>";
					}
				}else{
					id=udId;
					name = udName
					strHtml+="<option class='class1' name='selDepts' selected value='" + udId + "'>"
						+ udName + "</option>";
				}
				$("#selDepts").append(strHtml);
			})
		}
	});

	$("#delDept").click(function() {
		if ($("#selDepts option:selected").length > 0) {
			$("#selDepts option:selected").each(function() {
				var id = $(this).val();
				if(id.substring(0,2)=='DI'){
					alert("不能修改药监内部部门")
				}else{
					$(this).remove();
				}
			})
		}
	});
	// 选择角色的窗体上两个按钮的响应函数 addRole 和 delRole
	$("#addRole").click(function() {
		if ($("#subRoles option:selected").length > 0) {
			$("#subRoles option:selected").each(function() {
				var strHtml="";
				var rId = $(this).val();
				var rName = $(this).text();
				if ($("#selRoles option").length > 0 ) {
					var test = true;
					$("#selRoles option").each(function(){
						if(rId == $(this).val()){ 
							test = false;
						}
					})
					if(test){
						strHtml+="<option class='class1' name='selRoles' selected value='" + rId + "'>"
							+ rName + "</option>";
					}
				}else{
					strHtml+="<option class='class1' name='selRoles' selected value='" + rId + "'>"
						+ rName + "</option>";
				}
				$("#selRoles").append(strHtml);
				$(this).remove();
			})
		}
	});

	$("#delRole").click(function() {
		if ($("#selRoles option:selected").length > 0) {
			$("#selRoles option:selected").each(function() {
				var strHtml="";
				var rId = $(this).val();
				var rName = $(this).text();
				if ($("#subRoles option").length > 0 ) {
					var test = true;
					$("#subRoles option").each(function(){
						if(rId == $(this).val()){ 
							test = false;
						}
					})
					if(test){
						strHtml+="<option class='class1' name='subRoles' selected value='" + rId + "'>"
							+ rName + "</option>";
					}
				}else{
					strHtml+="<option class='class1' name='subRoles' selected value='" + rId + "'>"
						+ rName + "</option>";
				}
				$("#subRoles").append(strHtml);
				$(this).remove();
			})
		}
	});
	
	$("#deptType").change(function() {
		var dtId = $("#deptType option:selected").val();
		$.ajax({
  			type: "post",
  			data:{"dtId":dtId},
 			url: "admin/userDept_findUserDeptByDtId.action",
  			success:function(data){
  				$("#subDepts").empty();
  				$.each(data.userDepts,function(i,n){
  					$("#subDepts").append("<option value="+n.udId+">"+n.udName+"</option>");
				});
  			}
  		});
	});
	
});
//***************************************************czy


function dept_check(udId,udName,ckBox_id_dept){//注册点击CHECK BOX 的事件。
	var sign=0;
    var j_ckBox_id_dept = "#"+$.trim(ckBox_id_dept);

		var strHtml="";
$(j_ckBox_id_dept).click(function() {
		if($(j_ckBox_id_dept).attr("checked")==true){
		        if(sign=="1"){}
				else{
				if($("#selDepts option").length == 1){
					alert("只能选择一个部门！");
					$(j_ckBox_id_dept).attr("checked",false);
					return ;
				}}
		        
		     if ($("#selDepts option").length > 0 ) {
					var t = true;
					$("#selDepts option").each(function(){
						if(udId == $(this).val()){ 
							t = false;
						}
					});
					if(t){
						strHtml="<option class='class1' name='selDepts' selected value='" + udId + "'>"
							+ udName + "</option>";
					}
				}else{
					strHtml="<option class='class1' name='selDepts' selected value='" + udId + "'>"
						+ udName + "</option>";
				}
				$("#selDepts").append(strHtml);
			}
			else{//取消打钩，清楚右边的框
//				$("#selDepts").empty();
						$("#selDepts option").each(function(){
						if(udId == $(this).val()){ 
							$(this).remove();
						}
					});
			}
		});
}

function chgIcon(spanId){
	var span_id = "#" + $.trim(spanId);
	var imgsrc= $(span_id).children("img").attr("src");
	if(imgsrc=="/ag/wzp/images/jiahao.gif") {
		$(span_id).children("img").attr("src","/ag/wzp/images/jianhao.gif");
	}else{
		$(span_id).children("img").attr("src","/ag/wzp/images/jiahao.gif");}
}

//和selDeptAndRolet.js 对比 只修改了这个函数
function findAllDeptType(){
	var ckBoxId_dept;
	var extern = 0;
	$.ajax({     //ajax
		type: "post",
		url: "/ag/admin/userDept_findAllDeptType.action",
		success:function(data){
		$("#deptul2").empty();
		$.each(data.deptTypes,function(i,n){
//			alert(n.dtName);
		 extern++; 
		 var temp = "span_id_dup"+extern;
		var temp2 = "ul_id_dup"+extern;
		//if(n.dtId=="0001"||n.dtId=="0003"||n.dtId=="0002"){}   原来的
		if(n.dtId=="0000"||n.dtId=="0003"||n.dtId=="0002"){}   
		else{
		$("#deptul2").append("<li>"+"<span style=\"cursor: pointer;\" id="+temp+">"+"<img src=\"/ag/wzp/images/jiahao.gif\">"+n.dtName+"</span>"+"<ul style=\"list-style:none\" id="+temp2+">"+"</ul>"+"</li>");
		}
		var span_id = "#" + $.trim(temp);
		var ul_id = "#" + $.trim(temp2);
		$(span_id).children("img").attr("src","/ag/wzp/images/jiahao.gif");
		$(span_id).click(function() {
			chgIcon(temp);
			var deptTypeId = n.dtId;
//			alert(deptTypeId);
			$.ajax({     //ajax
	  			type: "post",
	  			data:{"deptTypeId":deptTypeId},
	 			url: "/ag/lwc/findFirstLevel_FindfDeptAction.action",
	  			success:function(data){
	  				if($(ul_id).text()!=""){
		  				$(ul_id).empty();
		  				$("#selDepts").empty();
		  			}else{
//		  			     alert(data.depts);
		  			     if(data.users!=null){
//		  			     alert(data.users);
		  			   //  setPeople(data.users,temp2);//tmpe2为ul 的id
		  			     }
		  				$.each(data.depts,function(i,n){
		  					extern++;
		  				var temp3 = "span2_id_dup"+extern;
  				     	var temp4 = "ul2_id_dup"+extern;
  				     	ckBoxId_dept = "ckBox_dept"+extern;
	  					$(ul_id).append("<li>"+"<span style=\"cursor: pointer;\" id="+temp3+">"+"<img src=\"/ag/wzp/images/jiahao.gif\">"+"</span>"+"<input type=\"checkbox\" id=\""+ckBoxId_dept+"\"/>"+n.udName+"<ul style=\"list-style:none\" id="+temp4+">"+"</ul>"+"</li>");
	  					dept_check(n.udId,n.udName,ckBoxId_dept);
	  					var span2_id = "#" + $.trim(temp3);
	  					var ul2_id = "#" + $.trim(temp4);
	  					$(span2_id).children("img").attr("src","/ag/wzp/images/jiahao.gif");
	  					$(span2_id).click(function() {
	  						chgIcon(temp3);
	  						var udid_1 = n.udId;
	  						$.ajax({
	  							type : "post",
	  				  			data:{"udid":udid_1},
	  				 			url: "/ag/lwc/nextLevel_FindfDeptAction.action",
	  				  			success:function(data){
	  				  			if($(ul2_id).text()!=""){
	  				  				$(ul2_id).empty();
//	  				  				$("#selDepts").empty();
	  				  				$.each(data.depts,function(i,n){
	  				  				$("#selDepts option").each(function(){
										if(n.udId == $(this).val()){ 
										$(this).remove();
										}
									});
	  				  				}
	  				  				);
	  				  			}else{
//	  				  			alert(data.depts);
	  				  		if(data.users!=null){
//	  			  			     alert(data.users);
	  			  			//  setPeople(data.users,temp4);//tmpe2为ul 的id
	  				  		}
	  				  		$.each(data.depts,function(i,n){
	  				  			extern++;
	  				  		var temp5 = "span3_id_dup"+extern;
	  				     	var temp6 = "ul3_id_dup"+extern;
	  				     	ckBoxId_dept = "ckBox_dept"+extern;
	  				     	$(ul2_id).append("<li>"+"<span style=\"cursor: pointer;\" id="+temp5+">"+"<img src=\"/ag/wzp/images/jiahao.gif\">"+"</span>"+"<input type=\"checkbox\" id=\""+ckBoxId_dept+"\"/>"+n.udName+"<ul style=\"list-style:none\" id="+temp6+">"+"</ul>"+"</li>");
	  				     	dept_check(n.udId,n.udName,ckBoxId_dept);
	  				     	var span3_id = "#" + $.trim(temp5);
		  					var ul3_id = "#" + $.trim(temp6);
		  					$(span3_id).children("img").attr("src","/ag/wzp/images/jiahao.gif");
		  					$(span3_id).click(function() {
		  						chgIcon(temp5);
		  					
		  						var udid_2 = n.udId;
		  						$.ajax({
		  							type : "post",
		  				  			data:{"udid":udid_2},
		  				 			url: "/ag/lwc/nextLevel_FindfDeptAction.action",
		  				  			success:function(data){
		  				  			if($(ul3_id).text()!=""){
		  				  				$(ul3_id).empty();
		  				  				$("#selDepts").empty();
		  				  			}else{
		  				  				if(data.depts!=null){
//		  				  			alert(data.depts);
		  				  				}
		  				  		if(data.users!=null){
//		  			  			     alert(data.users);
		  			  			//  setPeople(data.users,temp6);//tmpe6为ul 的id
		  				  		}
		  				  			}
		  				  			}
		  				  			});
		  						
		  					});
	  				  		});
	  				  				
	  				  			}
	  				  				
	  				  				
	  				  			}
	  							
	  						});

	  						
	  					});
		  					
		  				});
		  				}
			}
		});
			
			
		});
		
		});
		
	}
	});
	
}




//*****************************************************

//	//获取所有部门类型
//	function findAllDeptType(){
//		$.ajax({
//  			type: "post",
// 			url: "admin/userDept_findAllDeptType.action",
//  			success:function(data){
//  				$("#deptType").empty();
//  				$("<option value='0'>请选择</option>").appendTo("#deptType");
//  				$("#q_UDept").append("<option value='0'></option>");
//  				$.each(data.deptTypes,function(i,n){
//  					if(n.dtId!='0001'&&n.dtId!='0003')//过滤应急部门内部类型
//  						{$("<option value="+n.dtId+">"+n.dtName+"</option>").appendTo("#deptType");
//  						}else{
//  						}
//				});
//  			}
//  		});
//	}
	//获取所有角色
	function findAllRole(){
		$.ajax({
  			type: "post",
 			url: "admin/Role_findAllRole.action",//因为com.ag.admin.action.RoleAction的controller配的是RoleAction
  			success:function(data){
  				$("#subRoles").empty();
  				$.each(data.roles,function(i,n){
  					$("<option value="+n.RId+">"+n.RName+"</option>").appendTo("#subRoles");
				});
  			}
  		});
	}
	
	
	
	
	
	
	